package com.yanqu.road.dao.impl.activity.harps;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.harps.HarpsRewardInfo;
import com.yanqu.road.entity.activity.hell.HellNpc;
import com.yanqu.road.entity.activity.hell.HellUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HarpsRewardInfoDaoImpl extends TempDataObjectDao<HarpsRewardInfo> {

    public static String getHarpsRewardInfoSql = "select * from `t_u_activity_harps_reward` where `activityId`=? and `userId`=?";
    public Map<Integer, Map<Integer, HarpsRewardInfo>> getHarpsRewardInfo(int activityId, long userId) {
        Map<Integer, Map<Integer, HarpsRewardInfo>> ret = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getHarpsRewardInfoSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                HarpsRewardInfo temp = parse(rs);
                Map<Integer, HarpsRewardInfo> harpsRewardInfos = ret.computeIfAbsent(temp.getId(), k -> new ConcurrentHashMap<>());
                harpsRewardInfos.put(temp.getNo(), temp);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return ret;
    }

    public static String updateHarpsRewardInfoSql = "update `t_u_activity_harps_reward` set floor=?,seat=?,color=?,remainChooseNum=?,remainUnlockNum=?,isGetReward=?,isCurChoose=? where activityId=? and userId=? and id=? and no=?";
    @Override
    protected boolean updateData(HarpsRewardInfo harpsRewardInfo) {
        harpsRewardInfo.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getSeat()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getColor()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getRemainChooseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getRemainUnlockNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, harpsRewardInfo.getIsGetReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, harpsRewardInfo.getIsCurChoose()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getNo()));
        if (getDb().executeNoneQuery(updateHarpsRewardInfoSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHarpsRewardInfoSql = "insert into `t_u_activity_harps_reward`(activityId,userId,id,no,floor,seat,color,remainChooseNum,remainUnlockNum,isGetReward,isCurChoose) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HarpsRewardInfo harpsRewardInfo) {
        harpsRewardInfo.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getNo()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getSeat()));
        para.put(i++, new DbParameter(Types.INTEGER, harpsRewardInfo.getColor()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getRemainChooseNum()));
        para.put(i++, new DbParameter(Types.BIGINT, harpsRewardInfo.getRemainUnlockNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, harpsRewardInfo.getIsGetReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, harpsRewardInfo.getIsCurChoose()));
        if (getDb().executeNoneQuery(insertHarpsRewardInfoSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HarpsRewardInfo parseData(ResultSet rs) throws SQLException {
        HarpsRewardInfo harpsRewardInfo = new HarpsRewardInfo();
        harpsRewardInfo.setActivityId(rs.getInt("activityId"));
        harpsRewardInfo.setUserId(rs.getLong("userId"));
        harpsRewardInfo.setId(rs.getInt("id"));
        harpsRewardInfo.setNo(rs.getInt("no"));
        harpsRewardInfo.setFloor(rs.getInt("floor"));
        harpsRewardInfo.setSeat(rs.getInt("seat"));
        harpsRewardInfo.setColor(rs.getInt("color"));
        harpsRewardInfo.setRemainChooseNum(rs.getLong("remainChooseNum"));
        harpsRewardInfo.setRemainUnlockNum(rs.getLong("remainUnlockNum"));
        harpsRewardInfo.setIsGetReward(rs.getBoolean("isGetReward"));
        harpsRewardInfo.setIsCurChoose(rs.getBoolean("isCurChoose"));

        return harpsRewardInfo;
    }
}
